using System;

using CodingBlog.Core.Model;
using CodingBlog.Core.Repository;

using NUnit.Framework;

namespace CodingBlog.Core.Tests.Repository
{
    [TestFixture]
    internal class BlogRepositoryTests : BaseRepositoryTest
    {
        private static Blog GetSampleBlog(string testBlogTitle)
        {
            Blog blogToCreate = new Blog
            {
                Title = testBlogTitle,
                CreationDate = DateTime.Now
            };
            return blogToCreate;
        }

        [Test]
        public void after_creating_new_blog_blogs_count_in_db_equals_one()
        {
            string testBlogTitle = "test blog title";

            var count = new BlogRepository().GetBlogsCount();
            Assert.AreEqual(0, count);

            new BlogRepository().CreateBlog(GetSampleBlog(testBlogTitle));

            count = new BlogRepository().GetBlogsCount();
            Assert.AreEqual(1, count);
        }

        [Test]
        public void should_be_able_to_add_and_read_blog_from_database()
        {
            var blogRepository = new BlogRepository();

            var testBlogTitle = "test blog title";
            var blogToCreate = GetSampleBlog(testBlogTitle);

            var blogCreated = blogRepository.CreateBlog(blogToCreate);
            var blogToTest = blogRepository.GetBlogById(blogCreated.Id);

            Assert.NotNull(blogToTest);
            Assert.AreEqual(testBlogTitle, blogToTest.Title);
            Assert.GreaterOrEqual(blogToTest.Id, 1);
        }

        [Test]
        public void when_Database_empty_default_Blog_is_null()
        {
            var blogRepository = new BlogRepository();

            var blogToTest = blogRepository.GetDefaultBlog();
            Assert.Null(blogToTest);
        }

        [Test]
        public void can_get_default_blog()
        {
            var blogRepository = new BlogRepository();

            var testBlogTitle = "test blog title";
            var blogToCreate = GetSampleBlog(testBlogTitle);

            var blogCreated = blogRepository.CreateBlog(blogToCreate);
            var blogToTest = blogRepository.GetDefaultBlog();

            Assert.NotNull(blogToTest);
            Assert.AreEqual(blogCreated.Title, blogToTest.Title);
            Assert.AreEqual(blogCreated.Id, blogToTest.Id);
        }
    }
}
